package com.harmontronics.hanma.paas.paas.common;

import cn.hutool.core.collection.CollUtil;
import com.harmontronics.hanma.paas.paas.consts.BaseI18nKeyEnum;
import com.harmontronics.hanma.paas.paas.utils.RequestUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

/**
 * @author ldb
 */
@Data
public class ResponseEntity<T> {

    @ApiModelProperty(value = "执行结果状态码",required = true)
    private Integer code;

    @ApiModelProperty(value = "执行结果信息",required = true)
    private String message;

    @ApiModelProperty(value = "错误信息填充字段")
    private List<String> messageData;

    @ApiModelProperty(value = "返回正确结果时携带的数据")
    private T data;


    public static final Integer CODE_OK = 0;
    public static final String MESSAGE_OK = "hm.paas.ok";

    public static ResponseEntity<String> error() {
        return error(BaseI18nKeyEnum.SYSTEM_ERROR);
    }

    public static ResponseEntity<String> error(BaseI18nKeyEnum codeEnum) {
        ResponseEntity<String> resultBean = new ResponseEntity<>();
        resultBean.setCode(codeEnum.getCode());
        resultBean.setMessage(RequestUtils.localeMessage(codeEnum.getI18nKey()));
        return resultBean;
    }

    public static ResponseEntity<String> error(Integer code, String msg) {
        ResponseEntity<String> resultBean = new ResponseEntity<>();
        resultBean.setCode(code);
        resultBean.setMessage(msg);
        return resultBean;
    }

    public static ResponseEntity<String> error(Integer code, String msg, List<String> messageData) {
        ResponseEntity<String> resultBean = new ResponseEntity<>();
        resultBean.setCode(code);
        resultBean.setMessage(msg);
        resultBean.setMessageData(messageData);
        return resultBean;
    }

    public static ResponseEntity<String> error(BaseI18nKeyEnum i18nKeyEnum, List<String> messageData) {
        ResponseEntity<String> resultBean = new ResponseEntity<>();
        resultBean.setCode(i18nKeyEnum.getCode());
        resultBean.setMessage(i18nKeyEnum.getI18nKey());
        resultBean.setMessageData(messageData);
        return resultBean;
    }

    public static ResponseEntity<String> ok() {
        ResponseEntity<String> resultBean = new ResponseEntity<>();
        resultBean.setCode(CODE_OK);
        resultBean.setMessage(MESSAGE_OK);
        resultBean.setMessageData(CollUtil.newArrayList());
        return resultBean;
    }

    public static <T> ResponseEntity<T> ok(T data) {
        ResponseEntity<T> resultBean = new ResponseEntity<>();
        resultBean.setCode(CODE_OK);
        resultBean.setMessage(MESSAGE_OK);
        resultBean.setMessageData(CollUtil.newArrayList());
        resultBean.setData(data);
        return resultBean;
    }

    public static <T> ResponseEntity<T> create(Integer code,String message,T data){
        ResponseEntity<T> resultBean = new ResponseEntity<>();
        resultBean.setCode(code);
        resultBean.setMessage(message);
        resultBean.setMessageData(CollUtil.newArrayList());
        resultBean.setData(data);
        return resultBean;
    }
}
